﻿using System;
using System.Collections.Generic;

namespace OA.Model
{
    /// <summary>
    /// 来源于前端的请求对象，包含了响应的请求信息描述和对应的参数集合
    /// </summary>
    public sealed class JsonReq
    {
        private string _assemblyName;
        private string _className;
        private string _method;
        private Dictionary<string, string> _options;
        private string _filter;
        private List<NameValue> _arrayOptions;
        public string this[string str]
        {
            get { return this.Options[str]; }
            set { this.Options.Add(str, value); }
        }

        /// <summary>
        /// 程序集的名字，不带dll后缀
        /// </summary>
        public string AssemblyName
        {
            get { return _assemblyName; }
            set { _assemblyName = value; }
        }
        /// <summary>
        /// 请求的应用模块类名，包括完整的命名空间，即程序集的名字+类名
        /// </summary>
        public string ClassName
        {
            get { return _className; }
            set { _className = value; }
        }
        /// <summary>
        /// 请求的方法名称
        /// </summary>
        public string Method
        {
            get { return _method; }
            set { _method = value; }
        }
        /// <summary>
        /// 请求的参数集合
        /// </summary>
        public Dictionary<string, string> Options
        {
            get { return _options; }
            set { _options = value; }
        }

        /// <summary>
        /// 查询条件，一般的参数都在Options集合中，需要用特殊条件的如like ,between  and 
        /// 例如：name like '孔%'; birthday between '2011-12-01' and '2011-12-29'
        /// </summary>
        public string Filter
        {
            get { return _filter; }
            set { _filter = value; }
        }
        /// <summary>
        /// 从提交的请求参数集合中提取参数值
        /// </summary>
        /// <typeparam name="T">要提取的参数的类型</typeparam>
        /// <param name="name">参数名</param>
        /// <param name="zero">如果提取失败，使用的默认值</param>
        /// <returns>提取到的参数</returns>
        public T GetOption<T>(string name, T zero)
        {
            try
            {
                return (T)Convert.ChangeType(Options[name], typeof(T));
            }
            // ReSharper disable EmptyGeneralCatchClause
            catch (Exception)
            // ReSharper restore EmptyGeneralCatchClause
            { }
            return zero;
        }

        public List<Dictionary<string ,string>> ArrayOptions
        {
            get;
            set;
        }
    }
    public sealed class NameValue
    {
        public IDictionary<string, string> Options { get; set; }
    }
}